<?php
/*
	Aurus CMS - free OOP CMS
	Copyright (C) 2010  Nestor Yanchuk

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 2 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
class Plugin_select_template implements Plugin {
	public static function getName() {
		return 'Вибір оформлення';
	}
	public static function getVersion() {
		return '1.0';
	}
	public static function getDescription() {
		return __('Плагін дозволяє користувачу самому вибирати оформлення сайту');
	}
	public static function run() {
		if (isset($_GET['changeskin']) && is_file(ROOT . '/templates/' . $_GET['changeskin'] . '/main.tpl')) {
			set_cookie('template_to_load',$_GET['changeskin']);
			Aurus::location();
		}
		Filter::setFilter('Aurus', 'getTemplateToLoad', array(__CLASS__, 'getTemplateToLoad'));
		Template::getInstance()->set('{select_template}', self::getSelectList(), 'main');
	}
	public static function getTemplateToLoad() {
		if (isset($_COOKIE['template_to_load']) && is_dir(ROOT . '/templates/' . $_COOKIE['template_to_load'] . '/')) {
			return $_COOKIE['template_to_load'];
		} else {
			return Config::get('template');
		}
	}
	private static function getSelectList() {
		$sel = '';
		$tpl = read_directory(ROOT . '/templates/', 1);
		$sel .= '<option>' . __('Виберіть шаблон') . '</option>';
		foreach ($tpl as $template) {
			if (!file_exists(ROOT . '/templates/' . $template . '/main.tpl')) continue;
			$sel .= '<option value="' . $template . '">' . $template . '</option>';
		}
		return '<select onchange="document.location.href=\'/?changeskin=\' + this.options[ this.selectedIndex].value">' . $sel . '</select>';
	}
	
	public static function onActivate(){}
	public static function onDeactivate(){}
	public static function initAdminArea(){}
	public static function getHasAdmin(){return false;}
}
